<template>
  <div style="padding:15px">
    <vxe-toolbar id="toolbarId_fa3" :custom="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      show-footer
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :data="tableData"
      :sort-config="{ remote: true }"
      export-config
      show-overflow
      align="center"
      @sort-change="sortList"
    >
      <template v-for="(item,index) in columnList">
        <vxe-table-column :key="'colimn'+index">
          <vxe-table-column :field="item.field" :title="item.fieldName" min-width="200" />
          <template v-slot:header  v-if="item.type=='int'">
            <el-row>
              <el-col :span="11">
                <el-input type="number" v-model="listQuery[item.dbField+'_start']" placeholder="下限" clearable :disabled="item.source=='cal'" />
              </el-col>
              <el-col :span="11" :offset="2">
                <el-input type="number" v-model="listQuery[item.dbField+'_end']" placeholder="上限" clearable :disabled="item.source=='cal'"  />
              </el-col>
            </el-row>
          </template>
          <template  v-slot:header v-else-if="item.type=='date'">
             <el-date-picker
              v-model="listQuery[item.dbField]"
              type="daterange"
              value-format="yyyy-MM-dd"
              style="width:100%"
            range-separator="至"

              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="value => changeDate(value, item.dbField)"
           />
          </template>
            <template v-slot:header v-else>
            <el-input v-model="listQuery[item.dbField]" :placeholder="item.fieldName" :disabled="item.source=='cal'" clearable />
          </template>
        </vxe-table-column>
      </template>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-sizes="Pagingconf"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import request from "@/utils/request";
export default {
  name: "diyReport",
  components: {},
  data() {
    return {
      disabled:false,
      loading: false,
      columnList: [],
      tableData: [],
      listQuery: {
        code: "",
        branchCode: "",
        reportCode: "",
        pageNum: 1,
        pageSize: 20,
        sortField: "",
        sortType: "",
        total: 0
      },
      Pagingconf: []
    };
  },
  created() {
    this.listQuery.reportCode = this.$route.query.code;
    this.findColumnList();
  },
  methods: {
    //查询自定义列表
    findColumnList() {
      this.loading = true;
      return new Promise(resolve => {
        this.listQuery.reportCode = this.$route.query.code;
        request({
          url: "/wms/diyReportField",
          method: "get",
          params: this.listQuery
        }).then(response => {
          this.columnList = response.data.records;
          this.findList();
        }).catch(()=>{
          this.loading = false;
        });
        resolve();
      });
    },
    findList() {
      if (this.listQuery.hasOwnProperty("unload_quantity")) {
        if (this.listQuery.unload_quantity == "") {
          delete this.listQuery.unload_quantity;
        }
      }
      if (this.listQuery.hasOwnProperty("load_quantity")) {
        if (this.listQuery.load_quantity == "") {
          delete this.listQuery.load_quantity;
        }
      }
      this.loading = true;
      return new Promise(resolve => {
        // console.log(this.listQuery)
        request({
          url: "/wms/diyReport/selectReport",
          method: "get",
          params: this.listQuery
        }).then(response => {
          // console.log(response)
          this.tableData = response.data.records;
          this.tableData.forEach( i => {
            i.计划数量 = parseFloat(i.计划数量)
          })
          this.listQuery.total = response.data.total;
          this.Pagingconf = [20, 30, 50, 100];
          this.loading = false;
        }).catch(()=>{
          console.log('123');
          this.loading = false;
        });
        resolve();
      });
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
     // 时间
      changeDate(value, prefix) {
        if (value) {
          this.listQuery[`${prefix}_start`] = value[0] + " 00:00:00";
          this.listQuery[`${prefix}_end`] = value[1] + " 23:59:59";
        }
        else {
          this.listQuery[`${prefix}_start`] = "";
          this.listQuery[`${prefix}_end`] = "";
        }
      },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    }
  }
};
</script>

<style>
</style>
